#include "stdafx.h"
#include "cUrlFile.h"
#include <assert.h>
#include <iostream>
#include <list>
#define  COOKIE "C:\\Documents and Settings\\Administrator\\Cookies\\administrator@alibaba[1].txt"


struct FtpFile {
	LPCTSTR filename;
	FILE *stream;
};
struct	MEMORY_NODE
{
	BOOL	bLocked;
	UINT	nSize;
	BYTE*	pBuffer;
};
cUrlFile::cUrlFile(void)
{
	chunk.memory = NULL;
	chunk.size = 0 ;
}

cUrlFile::~cUrlFile(void)
{
	MemoryFree();
}

UINT cUrlFile::WriteMemory(LPVOID pData, UINT nCount, UINT nSize, LPVOID pObject)
{
	if(pData && nSize && pObject)
	{
		csMemory* pMemory = (csMemory*)pObject;
		if(pMemory->InsertData((const BYTE*)pData, nSize))
			return nSize;
	}
	return 1;
}

static int my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
{
	struct FtpFile *out=(struct FtpFile *)stream;
	if(out && !out->stream) {
		/* open file for writing */
		out->stream=fopen(out->filename, "wb");
		if(!out->stream)
			return -1; /* failure, can't open file to write */
	}
	//if (nmemb < (1024*3))
	//{
	//	fclose( out->stream );
	//	remove(out->filename);
	//	return 1;
	//}else{
		return fwrite(buffer, size, nmemb, out->stream);
	//}
}

 CURLcode cUrlFile::ToDownload_File(LPCTSTR saveFile)
 {
	 CURLcode ret;
	 CURL *curl_handle;
	 FILE* file = fopen( saveFile , "wb");
	 struct FtpFile ftpfile = {saveFile , file};
	 curl_global_init(CURL_GLOBAL_ALL);
	 curl_handle = curl_easy_init();
	 if (curl_handle)
	 {
		 curl_easy_setopt(curl_handle, CURLOPT_URL,  m_szURL );
		 //curl_easy_setopt(curl_handle, CURLOPT_HEADER, FALSE);
		 //curl_easy_setopt( curl_handle , CURLOPT_FOLLOWLOCATION, 1);
		 curl_easy_setopt( curl_handle , CURLOPT_WRITEFUNCTION, my_fwrite );
     	 curl_easy_setopt( curl_handle , CURLOPT_WRITEDATA, &ftpfile) ;
		 curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
		 curl_easy_setopt(curl_handle , CURLOPT_TIMEOUT_MS, 20000 );
		 curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT_MS, 20000);
		  //curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, TRUE);
		 ret = curl_easy_perform(curl_handle);
		 if (ftpfile.stream)
		 {
			 fclose( ftpfile.stream );
		 }
		 ULONG rcode = 0;
		 ret = curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE , &rcode); 
		 if(CURLE_OK == ret && rcode == 200)
		 {
			 CHAR* pContentType = NULL;
			 ret = curl_easy_getinfo(curl_handle, CURLINFO_CONTENT_TYPE, &pContentType);

			 if(CURLE_OK == ret && pContentType)
			 {
				 CHAR* pic= "image";
				 if (memicmp(pContentType , pic, strlen(pic) ) != 0)
				 {
					 remove( ftpfile.filename );
				 }
			 }
		 }else{
			 cout << rcode << endl;
			 remove(ftpfile.filename);
		 }
	 }

	curl_easy_cleanup(curl_handle);
	curl_global_cleanup();
	 return ret;
 }



 
 void *myrealloc(void *ptr, size_t size)
 {
 	if(ptr)
 		return realloc(ptr, size);
 	else
 		return malloc(size);
 }
 
 size_t
 WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
 {
 	size_t realsize = size * nmemb;
 	struct MemoryStruct *mem = (struct MemoryStruct *)data;
 
 	mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1);
 	if (mem->memory) {
 		memcpy(&(mem->memory[mem->size]), ptr, realsize);
 		mem->size += realsize;
 		mem->memory[mem->size] = 0;
 	}
 	return realsize;
 }
 
 CURLcode cUrlFile::ToDownload_Memory()
 {
	CURLcode ret;
	CURL *curl_handle;
	chunk.memory=NULL; /* we expect realloc(NULL, size) to work */
	chunk.size = 0;    /* no data at this point */

	curl_global_init(CURL_GLOBAL_ALL);

	/* init the curl_handle session */
	curl_handle = curl_easy_init();
	if (curl_handle)
	{
		curl_easy_setopt(curl_handle, CURLOPT_URL,  m_szURL );
		curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
		curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
		curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
		curl_easy_setopt(curl_handle , CURLOPT_TIMEOUT_MS, 20000 );
		curl_easy_setopt(curl_handle, CURLOPT_CONNECTTIMEOUT_MS, 20000);
		ret = curl_easy_perform(curl_handle);
		if(CURLE_OK == ret)
		{
			ULONG rcode = 0;
			ret = curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE , &rcode); 
			if(CURLE_OK == ret && rcode == 200)
			{
				CHAR* pContentType = NULL;
				ret = curl_easy_getinfo(curl_handle, CURLINFO_CONTENT_TYPE, &pContentType);

				if(CURLE_OK == ret && pContentType)
				{
					if(stricmp(pContentType, "text/html") == 0)
					{
						//UINT nSize = Memory.GetTotalSize();
						//if(nSize > 0)
						{
							BOOL bSaveToFile = TRUE;
							CHAR* pEffectiveUrl = NULL;
							ret = curl_easy_getinfo(curl_handle, CURLINFO_EFFECTIVE_URL, &pEffectiveUrl);
							if(CURLE_OK == ret && pEffectiveUrl)
							{
								if(_tcsicmp(m_szURL, pEffectiveUrl) != 0)
								{
									_tcscpy((LPTSTR)m_szURL, pEffectiveUrl);
								}
							}
						}
					}
				}

			}
		
		}
	curl_easy_cleanup(curl_handle);
	curl_global_cleanup();
	}
	return ret;
 }
 
 void cUrlFile::MemoryFree()
 {
	 if (chunk.memory!=NULL)
	 {
		 free( chunk.memory );
	 }
	 
 }

 LPCSTR cUrlFile::UrlMemData()
 {
	 return chunk.memory;
 }


 //ULONG rcode = 0;
 //ret = curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE , &rcode); 
 //if(CURLE_OK == ret && rcode == 200)
 //{
	// CHAR* pContentType = NULL;
	// ret = curl_easy_getinfo(curl_handle, CURLINFO_CONTENT_TYPE, &pContentType);

	// if(CURLE_OK == ret && pContentType)
	// {
	//	 if(stricmp(pContentType, "text/html") == 0)
	//	 {
	//		 UINT nSize = Memory.GetTotalSize();
	//		 if(nSize > 0)
	//		 {
	//			 BOOL bSaveToFile = TRUE;
	//			 CHAR* pEffectiveUrl = NULL;
	//			 ret = curl_easy_getinfo(curl_handle, CURLINFO_EFFECTIVE_URL, &pEffectiveUrl);
	//			 if(CURLE_OK == ret && pEffectiveUrl)
	//			 {
	//				 if(_tcsicmp(m_szURL, pEffectiveUrl) != 0)
	//				 {
	//					 _tcscpy(m_szURL, pEffectiveUrl);
	//					 int nURL = _tcslen(m_szURL);

	//					 //if(m_StoreFilt.Find(m_szURL, nURL))
	//					 //{
	//					 // bSaveToFile = FALSE;
	//					 //}
	//				 }
	//			 }

	//			 //BYTE* pData = MemAlloc(nSize);
	//			 //nSize = Memory.OutputData(pData, nSize);

	//			 //HrefExtract(m_szURL, pData, nSize);

	//			 //if(m_IndexFilt.Find(m_szURL, nURL))
	//			 //{
	//			 // bSaveToFile = FALSE;
	//			 //}
	//		 }
	//	 }
	// }

 //}